package com.xiaoh.tcp;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * @author xiaoH
 * @date 2020/4/23
 * @description
 */
public class LineServerHandler extends ChannelInboundHandlerAdapter {

    private int count = 0;
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg)
            throws Exception {

        count++;
        String body = (String) msg;
        System.out.println("server read msg : " + body +  ", count : " + count);

        String response = "hello from server"+System.getProperty("line.separator");
        ctx.writeAndFlush(response);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
            throws Exception {
        System.out.println("server caught exception" + cause);
        ctx.close();
    }

}
